package com.zoweunion.mechanic.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.zoweunion.mechanic.service.base.MessageRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.MessagePushService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;

@RestController
@RequestMapping("/messagePush")
public class MessagePushController extends BaseController{
	
	@Autowired
	private MessagePushService messagePushService;
	@Autowired
	private MessageRecordService messageRecordService;


	/**
	 * 获取消息详细根据用户
	 * @throws MyException
	 */
	@PostMapping("/getRecordListById")
	public ResponseBean getRecordListById(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送记录_详情据ID");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultMap=messageRecordService.getRecordListById(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}


	/**
	 * 获取消息根据用户
	 * @throws MyException
	 */
	@PostMapping("/recordListByReceiveUser")
	public ResponseBean recordListByReceiveUser(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送记录_消息列表据用户");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("receive_user", getCurrentUser(request).getId());
		List<Map<String, Object>> resultMap=messageRecordService.recordListByReceiveUser(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 获取消息数量根据用户
	 * @throws MyException
	 */
	@PostMapping("/recordNumberByReceiveUser")
	public ResponseBean recordNumberByReceiveUser(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送记录_消息数量据用户");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("receive_user", getCurrentUser(request).getId());
		Map<String, Object> resultMap=messageRecordService.recordNumberByReceiveUser(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}

	/**
	 * 获取消息推送记录
	 * @throws MyException
	 */
	@PostMapping("/recordList")
	public ResponseBean recordList(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送记录_列表展示");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		List<Map<String, Object>> resultMap=messageRecordService.getRecordList(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}
	
	/**
	 * 获取消息推送列表
	 * @throws MyException 
	 */
	@PostMapping("/list")
	public ResponseBean list(HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_列表展示");
		Map<String, Object> reqMap = new HashMap<String,Object>();
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		List<Map<String, Object>> resultMap=messagePushService.getPageQuery(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",resultMap);
	}
	
	/**
	 * 消息推送新增
	 */
	@PostMapping("/add")
	public ResponseBean add(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_新增");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		User currentUser = getCurrentUser(request);
		reqMap.put("s_id",currentUser.getS_id());
		reqMap.put("current_user_id",currentUser.getId());
		int result = messagePushService.insert(reqMap);
		logAfter(logger);
		if(result>0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
		}
		return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
	}
	
	/**
	 * 消息推送编辑
	 */
	@PostMapping("/edit")
	public ResponseBean edit(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_编辑");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("current_user_id",getCurrentUser(request).getId());
		int result = messagePushService.update(reqMap);
		logAfter(logger);
		if(result>0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
		}
		return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
	}
	/**
	 * 消息推送禁用
	 */
	@PostMapping("/ban")
	public ResponseBean ban(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_禁用");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("current_user_id",getCurrentUser(request).getId());
		reqMap.put("status",2);
		int result = messagePushService.update(reqMap);
		logAfter(logger);
		if(result>0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
		}
		return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
	}
	/**
	 * 消息推送启用
	 */
	@PostMapping("recover")
	public ResponseBean recover(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_启用");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("current_user_id",getCurrentUser(request).getId());
		reqMap.put("status",1);
		int result = messagePushService.update(reqMap);
		logAfter(logger);
		if(result>0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
		}
		return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
	}
	/**
	 * 消息推送删除
	 */
	@PostMapping("/delete")
	public ResponseBean delete(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "消息推送管理_删除");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("current_user_id",getCurrentUser(request).getId());
		reqMap.put("delete_flag",1);
		int result = messagePushService.update(reqMap);
		logAfter(logger);
		if(result>0) {
			return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
		}
		return new ResponseBean(Constants.CODE_HANDEL_FAILED,"操作失败",null);
	}
	
	
	
}
